<?php

class Admin_TaxonomyController extends Kieumau_Controller_Admin
{

    public function init()
    {
    	parent::init();
	}
    
	public function indexAction(){
		$this->_redirect('admin/taxonomy/list');
	}
	
	public function listAction(){
		
		$this->view->mainMenu 	= "Manage";
		$this->view->subMenu 	= "Taxonomy";
		$taxonomyTable = new Admin_Model_DbTable_Taxonomy();
		
		$categoryList = $taxonomyTable->getAllByTable();
		
		$page = $this->_getParam ( 'page', 1 );
		$paginator = Zend_Paginator::factory ( $categoryList );
		$paginator->setItemCountPerPage ( 4 );
		$paginator->setCurrentPageNumber ( $page );
		$this->view->taxonomyList = $paginator;
	}
	
  	public function addAction(){
    	$this->view->mainMenu = "Manage";
    	$this->view->subMenu = "Taxonomy";
    	$taxonomyTable = new Admin_Model_DbTable_Taxonomy();
    	
    	if($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		
    		$validateParams = array('name'=>'setRequired');
    		$error = $this->formValidate($formData, $validateParams);
    		
    		// If there's no error, The system will save data into database
    		if(count($error)==0){
    			$id = $taxonomyTable->insertTable($formData);//Insert tablee
    			$this->_redirect('admin/'.$this->getRequest()->getControllerName());
    		}else{//if exists error, fill form again and notice error
				$this->view->formData = $formData;
				$this->view->error = $error;
			}
    	}
    }
    
    public function editAction(){
    	$this->view->mainMenu = "Manage";
    	$this->view->subMenu = "Taxonomy";
    	
    	$id = $this->getRequest()->getParam('id',0);
    	$taxonomyTable = new Admin_Model_DbTable_Taxonomy();
    	
    	if($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    
    		$validateParams = array('name'=>'setRequired');
    		$error = $this->formValidate($formData, $validateParams);
    		
    		// If there's no error, The system will save data into database
    		if(count($error)==0){
    			$id = $taxonomyTable->updateTable($formData, (int)$id);//Insert table
    			$this->_redirect('admin/'.$this->getRequest()->getControllerName());
    		}else{//if exists error, fill form again and notice error
    			$this->view->formData = $formData;
    			$this->view->error = $error;
    		}
    	}else{
    		$formData = $taxonomyTable->getAllByTable('id = '.(int)$id);
    		$this->view->formData = $formData[0];
    	}
    }
    
    public function deleteAction(){
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender ();
    	
    	if($this->getRequest()->isPost()){
    		$id = $this->getRequest()->getPost('id');
    		$taxonomyTable = new Admin_Model_DbTable_Taxonomy();
    		$taxonomyTable->deleteTableLanguage($id);
    	}
    }
    
    public function activateAction(){
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender ();
    	if($this->getRequest()->isPost()){
    		$id = $this->getRequest()->getPost('id');
    		$taxonomyTable = new Admin_Model_DbTable_Taxonomy();
    		$taxonomyTable->activate($id);
    	}
    }
    
    public function detailAction(){
    	$this->view->mainMenu 	= "Manage";
    	$this->view->subMenu 	= "Taxonomy";
    	
    	$taxonomyId = $this->getRequest()->getParam('id');
    	
    	$taxonomyTable = new Admin_Model_DbTable_Taxonomy();
    	
    	//Get detail of this taxonomy
    	$taxonomyInfo = $taxonomyTable->getAllByTable('id = '.$taxonomyId);
		
    	//Get category inside this taxonomy
		$categoryTaxonomyTable = new Admin_Model_DbTable_CategoryTaxonomy();
    	$categoryList = $categoryTaxonomyTable->getCategory($taxonomyId, $this->defaultLanguageId);
		
    	//Assign to view
		$this->view->taxonomyInfo = $taxonomyInfo[0];
		$this->view->categoryList = $categoryList;
    	
    }
}